<?php
class ContentHotWidget extends HomeAction{

		//data[d]=天数;data[l]=调用条数;data[t]=栏目ID
		public function render($data){
			//$data['contentid']=$_GET['id'];
            $key=md5('contenthot'.$data['d'].$data['l'].$data['t']);
           
            $cache=S($key);

            if($cache!=null){
                $this->assign('ContentHot',$cache);
			}else{
                $contentDay=self::hitsSelect($data);
                $contentDay=$this->_url($contentDay);
				S($key,$contentDay,F('cachetime'));
				$this->assign('ContentHot',$contentDay);
			}
          	$this->display(dirname(__FILE__).'/Contenthot.html');
		}


		//内容排行
		/*参数1：按几天点击查询,参数2：数据调用条数*/
		/*返回:由内容ID组成的数组，可用于直接查询内容*/
		static protected function hitsSelect($data){

				//根据天数获得点击最高内容ID
				$hits=M('Hits');
				$day=time()-((3600*24)*$data['d']);	//几天前

				//栏目ID不存在则查询所有栏目
				if(!$data['t']){
					$res=$hits->query('select content_id,sum(hits) from __TABLE__ where date>'.$day.' group by content_id order by sum(hits) desc limit '.$data['l']);
					if(empty($res)){return null;}
				
				}else{

					//单个栏目查询条件
					if(is_numeric($data['t'])){
						$where='typeid='.$data['t'];
					}
					//多个栏目查询条件 
					if(is_string($data['t']) && strlen($data['t'])>1){
						$where='typeid in ('.$data['t'].')';
					}

					//执行查询	
					$res=$hits->query('select content_id from __TABLE__ where '.$where.' and date>'.$day.' group by content_id order by sum(hits) desc limit '.$data['l']);
					
					if(empty($res)){return null;}
				}

				foreach($res as $k=>$v){
					$ids[]=$v['content_id'];
				}

				//根据ID输出内容并返回
				$article=M('Article');

				foreach($ids as $k){
					$sql['where']=array('id'=>$k);
					$content[]=$article->where($sql['where'])->find($k);
				}

				return $content;	
		}

}
